package Engine;

import Creature.Food;
import Item.Item;

import java.io.DataOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;

public class GameSave {
    System1 sys;
    String filePath = "/1.txt";
    FileWriter fw = null;

    GameSave(System1 system1) {
        sys = system1;
    }

    public void save() {
        String data = sys.player.getMoney() + " " + sys.player.getSan() + " " + sys.player.getHungry() + " " + sys.player.getHealth() + " "
                + sys.player.getCharm() + " " + sys.player.getPower() + "　" + sys.player.getFatigue() + " " + sys.days + " " +
                sys.eventLine.keyEventIndex + " ";

        data +=sys.player.foods.size()+" ";
        for (Food food:sys.player.foods) {
            data+=food.getName()+" ";
        }

        data +=sys.player.itemList.size()+" ";

        for (Item item:sys.player.itemList) {
            data+=item.getName()+" ";
        }


        byte[] bytes = data.getBytes();
        try {
            write(bytes);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void write(byte[] bytes) throws Exception {
        File f = new File("res//stu.txt");//stu.txt创建在和src同级目录下的“data”文件夹中（和src目录并列）
        if (f.exists()) {
            f.delete();
        }
        f.createNewFile();
        FileOutputStream fos = new FileOutputStream(f);
        DataOutputStream dos = new DataOutputStream(fos);
        dos.write(bytes);
        dos.flush();
        dos.close();
    }
}


